/*
   Copyright 2011 Brook Smith

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.xempot.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class TestXmlContextEventReader {

	private static final String HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML = "/home/brook/Source/Qt1/act-public-2007-0097.xml";

	// private static final String
	// HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML =
	// "/home/brook/Source/Qt1/act-public-2007-0070.xml";
	// act-public-2007-0097.xml

	private XmlContextEventReader getXmlContextEventReader(InputStream in) throws XMLStreamException {
		javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newFactory();
		XMLEventReader reader = factory.createXMLEventReader(in);
		return new XmlContextEventReader(reader);
	}

	@Test
	public void testGetAttribute() {
		try {
			FileInputStream in = new FileInputStream(HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML);

			XmlContextEventReader contextEventReader = getXmlContextEventReader(in);
			assertNotNull("XmlContextEventReader", contextEventReader);

			while (contextEventReader.hasNext()) {
				XMLEvent event = contextEventReader.nextEvent();
				if (contextEventReader.getFlatPath().length() > 0) {
					assertNotNull("here " + contextEventReader.getFlatPath(), contextEventReader.getAttribute("id"));
					// System.out.println(contextEventReader.getAttribute("id"));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			assertNotNull(e);
		} catch (XMLStreamException e) {
			e.printStackTrace();
			assertNotNull(e);
		}
	}

	@Test
	public void testGetFlatPath() {
		try {
			FileInputStream in = new FileInputStream(HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML);

			XmlContextEventReader contextEventReader = getXmlContextEventReader(in);
			assertNotNull("XmlContextEventReader", contextEventReader);

			while (contextEventReader.hasNext()) {
				XMLEvent event = contextEventReader.nextEvent();
				assertNotNull("flatpath", contextEventReader.getFlatPath());
				// System.out.println(contextEventReader.getFlatPath());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			assertNotNull(e);
		} catch (XMLStreamException e) {
			e.printStackTrace();
			assertNotNull(e);
		}
	}

	@Test
	public void testGetXPathLocator() {
		try {
			FileInputStream in = new FileInputStream(HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML);

			XmlContextEventReader contextEventReader = getXmlContextEventReader(in);
			assertNotNull("XmlContextEventReader", contextEventReader);

			while (contextEventReader.hasNext()) {
				XMLEvent event = contextEventReader.nextEvent();
				assertNotNull("xpath", contextEventReader.getXPathLocator());
				// System.out.println(contextEventReader.getXPathLocator());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			assertNotNull(e);
		} catch (XMLStreamException e) {
			e.printStackTrace();
			assertNotNull(e);
		}
	}

}
